/**
 * 
 */
package info.niwota.install;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

/**
 * @author qiangli
 *
 */
public class InstallActivity extends Activity {

	private static final String TAG = "InstallActivity";

	private static final int RC_INSTALL = 1;

	public static final String EXTRA_PATH = "extra.path";
	
	private File file = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	
	    try {
	    
	    	final String path = getIntent().getStringExtra(EXTRA_PATH);
	    	file = new File(path);
	    	
	    	if (Debug.DEBUG) {
	    		Log.d(TAG, "file: " + file);
	    	}
	    	install();
	    } catch (Exception e) {
	    	e.printStackTrace();
	    	
	    	finish();
	    }
	}

	private void install() {
		Intent intent = new Intent();
		intent.setAction(android.content.Intent.ACTION_VIEW);
		intent.setDataAndType(Uri.fromFile(file),
				"application/vnd.android.package-archive");
		startActivityForResult(intent, RC_INSTALL);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (Debug.DEBUG) {
			Log.d(TAG, "requestCode: " + requestCode + " resultCode: " + resultCode + " data: " + data);
		}
		
		try {
			file.delete();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		finish();
	}
}
